package com.mxpio.erp.purchase.controller;


import com.mxpio.erp.common.purchase.entity.BuyRequest;
import com.mxpio.erp.common.purchase.service.BuyRequestService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.CriteriaUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Api(value = "BuyRequestController", tags = { "请购单数据接口" })
@RestController
@RequestMapping("/erp/purc/br/")
public class BuyRequestController {

    @Autowired
    private BuyRequestService buyRequestService;


    @GetMapping("list")
    @ApiOperation(value = "请购单列表", notes = "获取请购单（无分页）", httpMethod = "GET")
    public Result<List<BuyRequest>> list(String criteria) throws UnsupportedEncodingException {
        Criteria c = CriteriaUtils.json2Criteria(criteria);
        List<BuyRequest> buyRequests = buyRequestService.list(BuyRequest.class, c);
        return Result.OK(buyRequests);
    }

    @GetMapping("page")
    @ApiOperation(value = "请购单列表", notes = "获取请购单（分页）", httpMethod = "GET")
    public Result<Page<BuyRequest>> page(String criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Criteria c = CriteriaUtils.json2Criteria(criteria);
        Page<BuyRequest> buyRequests = buyRequestService.listPage(BuyRequest.class, page, c);
        return Result.OK(buyRequests);
    }

    @GetMapping("list/{bizNo}")
    @ApiOperation(value = "根据bizNo获取请购单", notes = "根据bizNo获取请购单", httpMethod = "GET")
    public Result<BuyRequest> getById(@PathVariable(name = "bizNo", required = true) String bizNo) {
        BuyRequest buyRequest = buyRequestService.getById(BuyRequest.class, bizNo);
        return Result.OK(buyRequest);
    }

    @PostMapping("add")
    @ApiOperation(value = "新增请购单", notes = "新增请购单", httpMethod = "POST")
    public Result<BuyRequest> add(@RequestBody BuyRequest buyRequest) {
        buyRequestService.save(buyRequest);
        return Result.OK(buyRequest);
    }

    @PutMapping("edit")
    @ApiOperation(value = "编辑请购单", notes = "编辑请购单", httpMethod = "PUT")
    public Result<BuyRequest> edit(@RequestBody BuyRequest buyRequest) {
        buyRequestService.update(buyRequest);
        return Result.OK(buyRequest);
    }

    @DeleteMapping("remove/{bizNo}")
    @ApiOperation(value = "根据bizNo删除请购单", notes = "根据bizNo删除请购单,多个bizNo用英文逗号分隔", httpMethod = "DELETE")
    public Result<BuyRequest> remove(@PathVariable(name = "bizNo", required = true) String bizNos) {
        String[] bizNo = bizNos.split(",");
        for(String key : bizNo){
            buyRequestService.remove(BuyRequest.class, key);
        }
        return Result.OK();
    }

    @GetMapping("purc/{bizNo}")
    @ApiOperation(value = "根据请购单bizNo生成采购单", notes = "根据请购单bizNo生成采购单,多个bizNo用英文逗号分隔", httpMethod = "GET")
    public Result<Object> purc(@PathVariable(name = "bizNo", required = true) String bizNos) {
        String msg = buyRequestService.purc(bizNos);
        return StringUtils.isBlank(msg)?Result.OK():Result.error(msg);
    }
}
